| Total Complexity | 9 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Type } from '../HumanResource/Leave/LeaveRequest.entity'; |
||
| 5 | |||
| 6 | export class GetFairCalendarOverview { |
||
| 7 | public index(items: ICalendar[]): ICalendarOverview { |
||
| 8 | const itemsByDate = []; |
||
| 9 | const overview: ICalendarOverview = { |
||
| 10 | mission: 0, |
||
| 11 | dojo: 0, |
||
| 12 | formationConference: 0, |
||
| 13 | leave: 0, |
||
| 14 | support: 0, |
||
| 15 | other: 0, |
||
| 16 | mealTicket: 0 |
||
| 17 | }; |
||
| 18 | |||
| 19 | for (const item of items) { |
||
| 20 | const dayIndex = new Date(item.getDate()).getDate() - 1; |
||
| 21 | const time = item.getTime() / 100; |
||
| 22 | const type = item.getType().startsWith('leave_') ? 'leave' : item.getType(); |
||
| 23 | |||
| 24 | if (itemsByDate[dayIndex]) { |
||
| 25 | itemsByDate[dayIndex].push({ time, type }); |
||
| 26 | } else { |
||
| 27 | itemsByDate[dayIndex] = [{ time, type }]; |
||
| 28 | } |
||
| 29 | |||
| 30 | overview[type] += time; |
||
| 31 | } |
||
| 32 | |||
| 33 | return { |
||
| 34 | ...overview, |
||
| 35 | mealTicket: this.getNumberOfMealTickets(Object.values(itemsByDate)) |
||
| 36 | }; |
||
| 37 | } |
||
| 38 | |||
| 39 | public getNumberOfMealTickets(itemsByDate: any[]): number { |
||
| 40 | let mealTicket = 0; |
||
| 41 | |||
| 42 | for (const sortedEvent of itemsByDate) { |
||
| 43 | let totalPerDay = 0; |
||
| 44 | |||
| 45 | for (const { time, type } of sortedEvent) { |
||
| 46 | if (type !== EventType.OTHER && 'leave' !== type) { |
||
| 47 | totalPerDay += time; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | if (totalPerDay > 0.5) { |
||
| 52 | mealTicket++; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | return mealTicket; |
||
| 57 | } |
||
| 59 |